using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace PopMail
{
    /// <summary>
    /// Container class that holds POP3 mail message details
    /// </summary>
    public class POP3MailMessage
    {
        private long _emailId;
        private string _from;
        private string _to;
        private string _subject;
        private string _body;
        private DateTime _mailReceivedDate;
        private long _messageSize;
        private long _inboxPosition;
        private string _contentType;
        private string _charset;
        private string _contentTransferEncoding;
        private bool _isMultiPart;
        private string _mailBoundary;
        private string _rawMessage;
        private string _messageSignature;

        private List<POP3MailAttachment> _attachments; //create 2 dimensional stuff - attachment is a ASCII stream with encoding.

        /// <summary>
        /// Public constructor of POP3MailMessage
        /// </summary>
        public POP3MailMessage()
        {
            _attachments = new List<POP3MailAttachment>();
            _emailId = 0;
            _inboxPosition = 0;
            _from = string.Empty;
            _to = string.Empty;
            _subject = string.Empty;
            _body = string.Empty;
            _mailReceivedDate = new DateTime();
            _messageSize = 0;
            _contentType = string.Empty;
            _charset = string.Empty;
            _contentTransferEncoding = string.Empty;
            _isMultiPart = false;
            _mailBoundary = string.Empty;
            _rawMessage = string.Empty;
            _messageSignature = string.Empty;
        }

        /// <summary>
        /// Email Id
        /// </summary>
        public long EmailId
        {
            get { return _emailId; }
            set { this._emailId = value; }

        }

        /// <summary>
        /// Unique signature of the message
        /// </summary>
        public string MessageSignature
        {
            get { return _messageSignature; }
            set { this._messageSignature = value; }
        }

        /// <summary>
        /// Raw message
        /// </summary>
        public string RawMessage
        {
            get { return _rawMessage; }
            set { this._rawMessage = value; }
        }

        /// <summary>
        /// Size of the message
        /// </summary>
        public long MessageSize
        {
            get { return _messageSize; }
            set { this._messageSize = value; }
        }

        /// <summary>
        /// From address
        /// </summary>
        public string From
        {
            get { return _from; }
            set { this._from = value; }
        }

        /// <summary>
        /// To address
        /// </summary>
        public string To
        {
            get { return _to; }
            set { this._to = value;}
        }

        /// <summary>
        /// Subject
        /// </summary>
        public string Subject
        {
            get { return _subject; }
            set { this._subject = value; }
        }

        /// <summary>
        /// Date the mail was received
        /// </summary>
        public DateTime MailReceivedDate
        {
            get { return _mailReceivedDate; }
            set { this._mailReceivedDate = value;}
        }
        
        /// <summary>
        /// Body of the mail message
        /// </summary>
        public string Body
        {
            get { return _body; }
            set { this._body = value; }
        }

        /// <summary>
        /// Method to add attachment into the mail
        /// </summary>
        /// <param name="attachment"></param>
        public void AddAttachment(POP3MailAttachment attachment)
        {
            this._attachments.Add(attachment);
        }

        /// <summary>
        /// Mail attachments
        /// </summary>
        public List<POP3MailAttachment> Attachments
        {
            get { return _attachments; }
        }
        
        /// <summary>
        /// Content type
        /// </summary>
        public string ContentType
        {
            get { return _contentType; }
            set { this._contentType = value; }
        }

        /// <summary>
        /// Character encoding of the message
        /// </summary>
        public string Encoding
        {
            get { return _contentTransferEncoding; }
            set { this._contentTransferEncoding = value; }
        }

        /// <summary>
        /// Character set used by the message
        /// </summary>
        public string Charset
        {
            get { return _charset; }
            set { this._charset = value; }
        }

        /// <summary>
        /// Message boundary
        /// </summary>
        public string Boundary
        {
            get { return _mailBoundary; }
            set { this._mailBoundary = value; }
        }

        /// <summary>
        /// Boolean flag indicating if the message is multi part
        /// </summary>
        public bool IsMultiPart
        {
            get { return _isMultiPart; }
            set { this._isMultiPart = value; }
        }

        /// <summary>
        /// Position of the mail in the inbox queue
        /// </summary>
        public long InboxPosition
        {
            get { return _inboxPosition; }
            set { this._inboxPosition = value; }
        }

        /// <summary>
        /// No of attachments in the Mail
        /// </summary>
        public int NoOfAttachments
        {
            get { return _attachments.Count; }
        }

        /// <summary>
        /// Method to check if the header retrieval is complete
        /// </summary>
        /// <returns></returns>
        public bool IsHeaderRetrievalComplete()
        {
            bool isCompleteFlag = false;

            if (_inboxPosition != 0
                &&  !_from.Equals(string.Empty)
                 &&  !_to.Equals(string.Empty)
                 //&&  !_subject.Equals(string.Empty)
                   &&  !_mailReceivedDate.Equals(new DateTime())
                    &&  _messageSize != 0
                     &&  !_contentType.Equals(string.Empty))
            {
                if (!_isMultiPart)
                {
                    isCompleteFlag = true;
                }


                if (_isMultiPart && !_mailBoundary.Equals(string.Empty))
                {
                    isCompleteFlag = true;
                }
            }

            return isCompleteFlag;
        }

        ///// <summary>
        ///// Method to set the database parameter list
        ///// </summary>
        ///// <returns></returns>
        //public ArrayList DbParamList()
        //{
        //    ArrayList paramList = new ArrayList();
            
        //    DBUtilities dbUtilities = new DBUtilities();

        //    paramList.Add(dbUtilities.CreateSqlParamater("@from", SqlDbType.NVarChar, 100, ParameterDirection.Input, this.From));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@to", SqlDbType.NVarChar, 100, ParameterDirection.Input, this.To));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@receivedDate", SqlDbType.DateTime,ParameterDirection.Input, this.MailReceivedDate));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@subject", SqlDbType.NVarChar, 255, ParameterDirection.Input, this.Subject));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@body", SqlDbType.NVarChar,4000, ParameterDirection.Input, this.Body));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@boundary", SqlDbType.NVarChar, 100, ParameterDirection.Input, this.Boundary));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@charset", SqlDbType.NVarChar,50, ParameterDirection.Input, this.Charset));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@contentType", SqlDbType.NVarChar,50, ParameterDirection.Input, this.ContentType));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@encoding", SqlDbType.NVarChar,50, ParameterDirection.Input, this.Encoding));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@inboxPosition", SqlDbType.Int, ParameterDirection.Input, this.InboxPosition));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@isMultipart", SqlDbType.Bit,ParameterDirection.Input, this.IsMultiPart));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@messageSize", SqlDbType.Int, ParameterDirection.Input, this.MessageSize));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@attachmentCount", SqlDbType.Int, ParameterDirection.Input, this.NoOfAttachments));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@messageSignature", SqlDbType.NVarChar,32, ParameterDirection.Input, this.MessageSignature));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@rawMessage",SqlDbType.NText, ParameterDirection.Input, this.RawMessage));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@insertStatus", SqlDbType.Bit, ParameterDirection.Output));
        //    paramList.Add(dbUtilities.CreateSqlParamater("@emailId",SqlDbType.Int,ParameterDirection.Output));

        //    dbUtilities = null; //Dispose off the utilities object before returning the list

        //    return paramList;
            
        //}

    }

}
